﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;

namespace XML
{
    public partial class frmTraCuu : Form
    {
        bool isMSSV = true;
        XmlDocument doc = new XmlDocument();

        public frmTraCuu()
        {
            InitializeComponent();
        }

        #region -----> Event <-----

        private void frmTraCuu_Load(object sender, EventArgs e)
        {
            rdMSSV.Checked = true;
            bool success = ValidateXml("QLSV.xml", "QLSV.xsd");
            if (!success)
            {
                MessageBox.Show("File XMl của bạn không hợp lệ! \nHãy kiểm tra lại.", "Thông báo", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("File XMl của bạn hợp lệ! \nBấm Ok để tiếp tục", "Thông báo", MessageBoxButtons.OK);
                ShowDataSV();
            }
        }

        private void dtGridSV_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in dtGridSV.SelectedCells)
            {
                cell = selectedCell;
                break;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                if (row.Cells["MSSV"].Value != null)
                {
                    ShowDataMH(row.Cells["MSSV"].Value.ToString());
                }
            }
        }

        private void rdMSSV_Click(object sender, EventArgs e)
        {
            isMSSV = true;

            btnSearch.Enabled = true;
            txtMSSV.Enabled = true;
            txtHoTen.Enabled = false;
            cbLop.Enabled = false;
        }

        private void rdHoTen_Click(object sender, EventArgs e)
        {
            isMSSV = false;

            btnSearch.Enabled = true;
            txtMSSV.Enabled = false;
            txtHoTen.Enabled = true;
            cbLop.Enabled = false;
        }

        private void rdLop_Click(object sender, EventArgs e)
        {
            btnSearch.Enabled = false;
            txtMSSV.Enabled = false;
            txtHoTen.Enabled = false;
            cbLop.Enabled = true;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (isMSSV)
            {
                string mssv = txtMSSV.Text;
                SearchMSSV(mssv);
            }
            else
            {
                string hoten = txtHoTen.Text;
                SearchHoTen(hoten);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //-----> Tìm kiếm theo lớp
        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string lop = cbLop.SelectedItem.ToString();
            SearchLop(lop);
        }

        #endregion

        #region -----> Method <-----

        //-----> Hàm kiêm tra file XML xem có hợp cấu trúc file schema không
        private bool ValidateXml(string xmlFilename, string schemaFilename)
        {
            // Tạo Validator
            XmlTextReader r = new XmlTextReader(xmlFilename);
            XmlValidatingReader validator = new XmlValidatingReader(r);
            validator.ValidationType = ValidationType.Schema;

            // Nạp Schema vào Validator
            XmlSchemaCollection schemas = new XmlSchemaCollection();
            schemas.Add(null, schemaFilename);
            validator.Schemas.Add(schemas);

            bool failed = false;

            try
            {
                doc.Load(validator);
            }
            catch (XmlSchemaException err)
            {
                failed = true;
            }
            finally
            {
                validator.Close();
            }

            return !failed;
        }

        //-----> Hàm hiển thị dữ liệu sinh viên
        private void ShowDataSV()
        {
            dtGridSV.Rows.Clear();
            int i = 0;
            foreach (XmlNode node in doc.ChildNodes[1].ChildNodes)
            {
                dtGridSV.Rows.Add();
                dtGridSV.Rows[i].Cells["MSSV"].Value = node.ChildNodes[0].InnerText;
                dtGridSV.Rows[i].Cells["HoTen"].Value = node.ChildNodes[1].InnerText;
                dtGridSV.Rows[i].Cells["Lop"].Value = node.ChildNodes[2].InnerText;
                i++;
            }
        }

        //-----> Hàm hiển thị dữ liệu môn học
        private void ShowDataMH(string mssv)
        {
            dtGridMH.Rows.Clear();
            XmlNode node = doc.SelectSingleNode("/ListStudent/Student[MSSV = " + mssv + "]");

            foreach (XmlNode Node in node.ChildNodes)
            {
                if (Node.Name == "ListSubject")
                {
                    int i = 0;
                    foreach (XmlNode subNode in Node.ChildNodes)
                    {
                        dtGridMH.Rows.Add();
                        foreach (XmlNode sub in subNode.ChildNodes)
                        {
                            if (sub.Name == "NameSub")
                            {
                                dtGridMH.Rows[i].Cells["TenMon"].Value = sub.InnerText;
                            }

                            if (sub.Name == "SoTC")
                            {
                                dtGridMH.Rows[i].Cells["SoTC"].Value = sub.InnerText;
                            }

                            if (sub.Name == "Diem")
                            {
                                dtGridMH.Rows[i].Cells["Diem"].Value = sub.InnerText;
                            }
                        }
                        i++;
                    }
                }
            }
        }

        //-----> Tìm kiếm theo lớp
        private void SearchLop(string lop)
        {
            dtGridSV.Rows.Clear();
            dtGridMH.Rows.Clear();
            XmlNodeList node = doc.SelectNodes("/ListStudent/Student[Lop = '" + lop + "']");
            int j = 0;

            foreach (XmlNode Node in node)
            {
                dtGridSV.Rows.Add();
                dtGridSV.Rows[j].Cells["MSSV"].Value = Node.ChildNodes[0].InnerText;
                dtGridSV.Rows[j].Cells["HoTen"].Value = Node.ChildNodes[1].InnerText;
                dtGridSV.Rows[j].Cells["Lop"].Value = Node.ChildNodes[2].InnerText;

                if (j == 0)
                {
                    int i = 0;
                    foreach (XmlNode subNode in Node.ChildNodes[3].ChildNodes)
                    {
                        dtGridMH.Rows.Add();
                        foreach (XmlNode sub in subNode.ChildNodes)
                        {
                            if (sub.Name == "NameSub")
                            {
                                dtGridMH.Rows[i].Cells["TenMon"].Value = sub.InnerText;
                            }

                            if (sub.Name == "SoTC")
                            {
                                dtGridMH.Rows[i].Cells["SoTC"].Value = sub.InnerText;
                            }

                            if (sub.Name == "Diem")
                            {
                                dtGridMH.Rows[i].Cells["Diem"].Value = sub.InnerText;
                            }
                        }
                        i++;
                    }
                }
                j++;
            }
        }

        //-----> Tìm kiếm theo MSSV
        private void SearchMSSV(string mssv)
        {
            dtGridSV.Rows.Clear();
            dtGridMH.Rows.Clear();
            XmlNode node = doc.SelectSingleNode("/ListStudent/Student[MSSV = " + mssv + "]");

            if (node != null)
            {
                dtGridSV.Rows[0].Cells["MSSV"].Value = node.ChildNodes[0].InnerText;
                dtGridSV.Rows[0].Cells["HoTen"].Value = node.ChildNodes[1].InnerText;
                dtGridSV.Rows[0].Cells["Lop"].Value = node.ChildNodes[2].InnerText;

                foreach (XmlNode Node in node.ChildNodes)
                {
                    if (Node.Name == "ListSubject")
                    {
                        int i = 0;
                        foreach (XmlNode subNode in Node.ChildNodes)
                        {
                            dtGridMH.Rows.Add();
                            foreach (XmlNode sub in subNode.ChildNodes)
                            {
                                if (sub.Name == "NameSub")
                                {
                                    dtGridMH.Rows[i].Cells["TenMon"].Value = sub.InnerText;
                                }

                                if (sub.Name == "SoTC")
                                {
                                    dtGridMH.Rows[i].Cells["SoTC"].Value = sub.InnerText;
                                }

                                if (sub.Name == "Diem")
                                {
                                    dtGridMH.Rows[i].Cells["Diem"].Value = sub.InnerText;
                                }
                            }
                            i++;
                        }
                    }
                }
            }
        }

        //-----> Tìm kiếm theo Họ tên
        private void SearchHoTen(string hoten)
        {
            dtGridSV.Rows.Clear();
            dtGridMH.Rows.Clear();
            XmlNode node = doc.SelectSingleNode("/ListStudent/Student[HoTen = '" + hoten + "']");

            if (node != null)
            {
                dtGridSV.Rows[0].Cells["MSSV"].Value = node.ChildNodes[0].InnerText;
                dtGridSV.Rows[0].Cells["HoTen"].Value = node.ChildNodes[1].InnerText;
                dtGridSV.Rows[0].Cells["Lop"].Value = node.ChildNodes[2].InnerText;

                foreach (XmlNode Node in node.ChildNodes)
                {
                    if (Node.Name == "ListSubject")
                    {
                        int i = 0;
                        foreach (XmlNode subNode in Node.ChildNodes)
                        {
                            dtGridMH.Rows.Add();
                            foreach (XmlNode sub in subNode.ChildNodes)
                            {
                                if (sub.Name == "NameSub")
                                {
                                    dtGridMH.Rows[i].Cells["TenMon"].Value = sub.InnerText;
                                }

                                if (sub.Name == "SoTC")
                                {
                                    dtGridMH.Rows[i].Cells["SoTC"].Value = sub.InnerText;
                                }

                                if (sub.Name == "Diem")
                                {
                                    dtGridMH.Rows[i].Cells["Diem"].Value = sub.InnerText;
                                }
                            }
                            i++;
                        }
                    }
                }
            }
        }

        #endregion

    }
}
